package cn.cit.training.data.controller;

import cn.cit.training.data.entity.CitData;
import cn.cit.training.data.vo.ChartInfo;
import cn.cit.training.data.vo.CommonResult;
import cn.cit.training.data.service.CitDataService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;

/**
 * <p>
 * 数据表 前端控制器
 * </p>
 *
 */
@Slf4j
@RestController
public class CitDataController {

    @Resource
    private CitDataService citDataService;

    /**
     * status
     *
     * @return
     */
    @ApiOperation("状态检查")
    @PutMapping("/")
    public CommonResult status() {
        try {
            return CommonResult.success("true");
        } catch (Exception e) {
            log.error(e.getMessage());
            return new CommonResult(e);
        }
    }

    /**
     * 查询CitData列表
     *
     * @param pageSize 每页记录数
     * @param currentPage 当前页码
     * @param field 查询字段
     * @param min 区间最小
     * @param max 区间最大
     * @return
     */
    @ApiOperation("查询列表")
    @GetMapping("/citDatas")
    public CommonResult<IPage<CitData>> getCitDataPage(
            @RequestParam(value = "pageSize", required = false, defaultValue = "10")
            @ApiParam(value = "每页记录数", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "currentPage", required = false, defaultValue = "1")
            @ApiParam(value = "当前页码", required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(value = "field", required = false, defaultValue = "")
            @ApiParam(value = "查询字段：year, t_travel, t_duration", required = false ,example = "t_duration") String field,
            @RequestParam(value = "min", required = false, defaultValue = "0")
            @ApiParam(value = "区间最小", required = false, defaultValue = "0" ,example = "1971") Integer min,
            @RequestParam(value = "max", required = false, defaultValue = "0")
            @ApiParam(value = "区间最大", required = false, defaultValue = "0" ,example = "1980") Integer max) {
        try {

            // 查询数据库
            IPage<CitData> page = citDataService.getCitDataPage(pageSize, currentPage, field, min, max);
            ChartInfo chartInfo = new ChartInfo();
            if(min != 0 && max != 0){
                chartInfo = citDataService.getChartData(min, max);
            }

            // 组装数据
            return CommonResult.success(page, chartInfo);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new CommonResult(e);
        }
    }

    /**
     * 查询CitData
     *
     * @param id 唯一主键 _id
     * @return
     */
    @ApiOperation("根据_id查询单个数据")
    @GetMapping("/citDatas/{id}")
    public CommonResult<CitData> getCitDataById(@PathVariable("id") Integer id) {
        try {
            // 查询数据库
            CitData CitData = citDataService.getOneById(id);

            // 组装数据
            return CommonResult.success(CitData);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new CommonResult(e);
        }
    }

    /**
     * 添加CitData
     *
     * @param citData
     * @return
     */
    @ApiOperation("添加数据")
    @PostMapping("/citDatas")
    public CommonResult<CitData> createCitData(@RequestBody CitData citData) {
        try {
            // 查询数据库
            citDataService.save(citData);

            return CommonResult.success(citData);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new CommonResult(e);
        }
    }

    /**
     * 删除CitData
     *
     * @param id
     * @return
     */
    @ApiOperation("删除数据")
    @DeleteMapping("/citDatas/{id}")
    public CommonResult<Boolean> deleteCitData(@PathVariable("id") Integer id) {
        try {
            // 查询数据库
            boolean flag = citDataService.deleteCitData(id);

            return CommonResult.success(flag);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new CommonResult(e);
        }
    }

    /**
     * 更新CitData
     *
     * @param id
     * @param citData
     * @return
     */
    @ApiOperation("更新数据")
    @PutMapping("/citDatas/{id}")
    public CommonResult<Boolean> updateCitData(@PathVariable("id") Integer id, @RequestBody CitData citData) {
        try {
            citData.setId(id);
            // 查询数据库
            boolean flag = citDataService.updateCitData(citData);
            return CommonResult.success(flag);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new CommonResult(e);
        }
    }

}

